layui.use(["jquery", "upload", "form", "layer", "element"], function () {
    var $ = layui.$,
        element = layui.element,
        layer = layui.layer,
        upload = layui.upload,
        form = layui.form;
    var s = "";
    var user = JSON.parse(localStorage.getItem('user'));
    $('input[name = createId]').val(user.userId);
    $('#test1').on("click",function () {
        $('#showBtn').html('<div  class="layui-upload-list" id="demo2" style="margin-left: 120px"></div>');
    });

    //刷新
    function refresh() {
        window.location.reload();
    }

    //上传
    var i = 0;
    var imgNum = 1;
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '/Upload/upload'
        ,accept: 'images'
        ,field:'file'
        ,bindAction:'#subUpload'
        ,auto:false
        ,size: 1000
        ,multiple: true
        ,number: 9
        ,choose:function (obj) {
            var files = obj.pushFile();

            obj.preview(function(index, file, result){
                $('#demo2').append('<div class="img_div" id="remove_'+index+'"><img src="' + result
                    + '" alt="' + file.name
                    + '"height="100px" width="100px" class="layui-upload-img uploadImgPreView"></div>');


                $('#remove_' + index).bind('dblclick', function () {//双击删除指定预上传图片
                    delete files[index];//删除指定图片
                    $(this).remove();
                });
            });
        }
        , done: function (res) {
                $('input[name = mainImage]').val(res.data);
                if(i <= 0){
                    $('#sub').click();
                    i++;
                }
            if (s == "") {
                s = JSON.stringify(res);
            } else {
                s = s + "," + JSON.stringify(res);
            };
            localStorage.setItem("address1", s);
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                //layer.msg('上传完毕', {icon: 1});
            }
        }
    });




    findBookName = function findBookName(){
        layer.open({
            type:2,
            content:"/Manageproductlist/productlist-category.html",
            area:['670px','500px'],
        });
    }

    //商品推荐
    form.on('radio(recommend)', function(data){
        $('#recommend').val(data.value);
    });
    //是否可用优惠卷
    form.on('radio(discount)', function(data){
        $('#discount').val(data.value);
    });
    //服务保证
    form.on('radio(ensure)', function(data){
        $('#ensure').val(data.value);
    });


    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:'/Product/addProduct',
            dataType:'json',
            data:data.field,
            success:function (data) {
                if(data.data){
                    //添加的操作日志
                    $.ajax({
                        url:'/Product/addProductOperationRecord',
                        dataType:'json',
                        data:{
                            "dictValue":"添加反馈日志","cssClass":"添加","createBy":$('#proId',window.parent.document).val(),
                            "remark":"无内容"
                        }
                    });
                    layer.msg("添加成功");

                    $(window.parent.document).find('#tiaoCommodityList').click();
                }else{
                    layer.msg("添加失败");
                }
            }
        });
        return false;
    });
});

function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);
}